# -*- mode: python -*-
###
#
# (c) 2000-2007 Wijnand Modderman <wijnand at tehmaze dot com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

__revision__ = '$Hg: ssl.py,v afe935628711 2006/12/31 03:53:14 +0100 $'

# system imports
from OpenSSL import SSL, crypto

# DDB imports
from ddb.log import log

# Twisted imports
from twisted.internet import ssl

class SSLContext(ssl.DefaultOpenSSLContextFactory):
    def __init__(self, key='config/ddb.pem', cert='config/ddb.pem', cacerts=['config/ddb.pem']):
        self.mKey  = crypto.load_privatekey (crypto.FILETYPE_PEM, open(key, 'r').read())
        self.mCert = crypto.load_certificate(crypto.FILETYPE_PEM, open(cert, 'r').read())
        self.mCACert = [crypto.load_certificate(crypto.FILETYPE_PEM, open(x, 'r').read()) for x in cacerts]
        
    def verify(self, conn, cert, errnum, depth, ok):
        """Checks the certificate of an incoming connection"""
        return 1 # we don't care :-)
        
    def getContext(self):
        """Create an SSL context."""
        ctx = SSL.Context(SSL.SSLv2_METHOD)
        # Add the CA certificate(s)
        store = ctx.get_cert_store()
        for cert in self.mCACert:
            store.add_cert(cert)
        # Load the private key and certificate
        ctx.use_privatekey(self.mKey)
        ctx.use_certificate(self.mCert)
        ctx.set_verify(SSL.VERIFY_PEER | 
                SSL.VERIFY_FAIL_IF_NO_PEER_CERT, self.verify)
        ctx.set_verify_depth(len(self.mCACert))
        return ctx

# vim:ts=4:sw=4:et:
